package com.example.demo.common;

/**
 * Description:
 * User: 故我
 * Date: 2023-11-09
 * Time: 11:44
 */

import com.example.demo.entity.UserInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 用户登录信息的公共类
 */
public class UserSessionUtils {
    // 从登录传入的cookie,从session中获取到用户信息
    public static UserInfo getUser(HttpServletRequest request) {
        HttpSession session = request.getSession(false);//不自动创建
        if (session != null &&
                session.getAttribute(ApplicationVal.USER_SESSION_KEY) != null) {
            // 能够获取到用户的信息
            return (UserInfo) session.getAttribute(ApplicationVal.USER_SESSION_KEY);
        }
        return null;
    }
}
